package com.bondex.charter.service;


import com.bondex.charter.commons.MailUtil;
import com.bondex.charter.dao.CharterBookingMapper;
import com.bondex.charter.dao.CharterSiMapper;
import com.bondex.charter.maindata.pojo.api.BaseComContainerTypeVO;
import com.bondex.charter.maindata.pojo.dto.BaseComContainerTypeDTO;
import com.bondex.charter.po.CharterBooking;
import com.bondex.charter.po.CharterSi;
import com.bondex.charter.pojo.*;
import com.bondex.charter.pojo.excelvo.EntrustVO;
import com.bondex.charter.pojo.excelvo.ISFVO;
import com.bondex.charter.pojo.excelvo.LoadVO;
import com.bondex.global.pojo.PageTableParamT;
import com.bondex.global.pojo.ResultT;
import com.bondex.global.utils.DateUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @author： wh
 * @date: 2021/9/16 15:55
 */
@Service
public class ExcelService extends BaseService {

    @Autowired
    private CharterSiMapper charterSiMapper;

    @Autowired
    private CharterBookingMapper charterBookingMapper;

    @Autowired
    private ListService listService;

    @Autowired
    MainDataCacheService mainDataCache;

    protected Logger log = LoggerFactory.getLogger(ExcelService.class);

    /**
     * 获取配载明细列表
     * @return 配载明细列表
     */
    public List<LoadVO> getLoadList(String token) {
        List<CharterBooking> charterBookingList = charterBookingMapper.selectForLoad();
        List<LoadVO> loadvoList = new ArrayList<>();
        //遍历得到的截单charterBookingList列表，复制到导出loadvoList列表
        charterBookingList.forEach(charterBooking -> {
            //解析ContainerInfo判断箱型箱量的数量
            Gson gson = new Gson();
            List<ContainerInfo> containerInfoList = gson.fromJson((String) charterBooking.getContainerInfo(), new TypeToken<List<ContainerInfo>>() {
            }.getType());
            for (ContainerInfo containerInfo : containerInfoList){
                LoadVO loadVO = new LoadVO();
                //设置LoadVO固定信息
                loadVO.setVesselVoyage(charterBooking.getVessel()+ " V." + charterBooking.getVoyage());
                loadVO.setContainerType(containerInfo.getContainerType());
                loadVO.setBlNo(charterBooking.getBlNo());
                loadVO.setContainerNo(containerInfo.getContainerNo());
                loadVO.setSealNo(containerInfo.getSealNo());
                loadVO.setPortOfLoadingName(charterBooking.getPortOfLoadingName());
                loadVO.setPortOfTransshipmentName(charterBooking.getPortOfTransshipmentName());
                loadVO.setPortOfDischargeName(charterBooking.getPortOfDischargeName());
                loadVO.setWeight(containerInfo.getWeight());
                if (null != containerInfo.getPackageQty()){
                    loadVO.setPackageQty(new BigDecimal(containerInfo.getPackageQty()));
                }
                loadVO.setTareWeight(containerInfo.getTareWeight());
                if (null != containerInfo.getWeight() && null != containerInfo.getTareWeight()) {
                    loadVO.setWeightAndtareWeight(containerInfo.getWeight().add(containerInfo.getTareWeight()));
                }
                loadVO.setDescription(charterBooking.getDescription());
                //获取teu数据，根据箱型代码判断找到对应箱子，设置teu
                List<BaseComContainerTypeVO> baseComContainerTypeVOS = mainDataCache.getBaseComContainerTypeDTOList(token);
                if (null != baseComContainerTypeVOS) {
                    for (BaseComContainerTypeVO baseComContainerTypeVO : baseComContainerTypeVOS) {
                        if (containerInfo.getContainerTypeNo().equals(baseComContainerTypeVO.getContainerTypeNo())) {
                            loadVO.setTeu(baseComContainerTypeVO.getEquality());
                        }
                    }
                }
                loadvoList.add(loadVO);
            }
        });
        return loadvoList;
    }

    /**
     * 获取ISF列表
     * @return ISF列表
     */
    public List<ISFVO> getISFList() {
        List<CharterSi> charterSiList = charterSiMapper.selectForISF();
        List<ISFVO> isfvoList = new ArrayList<>();
        //遍历得到的截单charterSi列表，复制到导出ISFVO列表
        charterSiList.forEach(charterSi -> {
            //解析getContainerInfo判断箱型箱量的数量
            Gson gson = new Gson();
            List<ContainerInfo> containerInfoList = gson.fromJson((String) charterSi.getContainerInfo(), new TypeToken<List<ContainerInfo>>() {
            }.getType());
            for (ContainerInfo containerInfo : containerInfoList){
                ISFVO isfvo = new ISFVO();
                //设置ISFVO固定信息
                isfvo.setBay("");
                isfvo.setRow("");
                isfvo.setTier("");
                isfvo.setScac("BXAB");
                isfvo.setIsContainerEmpty("NO");
                isfvo.setHazmatNumber("");
                isfvo.setVessel(charterSi.getVessel());
                isfvo.setVoyage(charterSi.getVoyage());
                isfvo.setGrossWeight(containerInfo.getWeight());
                isfvo.setPortOfLoadingName(charterSi.getPortOfLoadingName());
                isfvo.setPortOfDischargeName(charterSi.getPortOfDischargeName());
                isfvo.setContainerType(containerInfo.getContainerType());
                isfvo.setContainerNo(containerInfo.getContainerNo());
                isfvoList.add(isfvo);
            }
        });
        return isfvoList;
    }
    public List<EntrustVO> getEntrustList(String token,List<String> list) {
        //设置查询条件，每页N(列表数量)条，查询第一页
        PageTableParamT<SearchParam> param = new PageTableParamT<SearchParam>();
        param.setPageCount(list.size());
        param.setPageNumber(0);
        //组装查询参数，订单号用，隔开
        StringBuilder orderNos = new StringBuilder("");
        for (String orderNo : list) {
            orderNos.append(",").append(orderNo);
        }
        SearchParam searchParam = new SearchParam();
        searchParam.setOrderNos(orderNos.toString());
        param.setSearchParam(searchParam);
        //查询需要导出的数据
        ResultT<List<OrderInfo4List>> resultT = listService.getList(param);
        List<OrderInfo4List> orderInfo4Lists = resultT.getData();
        List<EntrustVO> entrustVOList = new ArrayList<>();
        //将得到的委托数据orderInfo4Lists格式化到entrustVOList当中
        orderInfo4Lists.forEach(orderInfo4List -> {
            EntrustVO entrustVO = new EntrustVO();
            entrustVO.setOrderNo(orderInfo4List.getOrderNo());
            entrustVO.setBlNo(orderInfo4List.getBlNo());
            entrustVO.setCustomerNo(orderInfo4List.getCustomerNo());
            entrustVO.setContainerNoAndType(orderInfo4List.getContainerNoAndType());
            entrustVO.setOpRemark(orderInfo4List.getOpRemark());
            //箱信息需要解析ContainerInfo
            Gson gson = new Gson();
            List<ContainerInfo> containerInfoList = gson.fromJson((String) orderInfo4List.getContainerInfo(), new TypeToken<List<ContainerInfo>>() {
            }.getType());
            StringBuilder containerNoAndSealNoAndType = new StringBuilder("");
            BigDecimal tareWeight = new BigDecimal(0);
            entrustVO.setTeu(new BigDecimal(0));
            for (ContainerInfo containerInfo : containerInfoList) {
                //拼接箱号/封号/箱型
                containerNoAndSealNoAndType.append(containerInfo.getContainerNo())
                        .append("/")
                        .append(containerInfo.getSealNo())
                        .append("/")
                        .append(containerInfo.getContainerType())
                        .append(" ");
                //计算总皮重
                if (null != containerInfo.getTareWeight()){
                    tareWeight = tareWeight.add(containerInfo.getTareWeight());
                }
                //获取teu数据，根据箱型代码判断找到对应箱子，设置teu
                List<BaseComContainerTypeVO> baseComContainerTypeVOS = mainDataCache.getBaseComContainerTypeDTOList(token);
                if (null != baseComContainerTypeVOS) {
                    for (BaseComContainerTypeVO baseComContainerTypeVO : baseComContainerTypeVOS) {
                        if (containerInfo.getContainerTypeNo().equals(baseComContainerTypeVO.getContainerTypeNo())) {
                            BigDecimal teu = new BigDecimal(baseComContainerTypeVO.getEquality());
                            entrustVO.setTeu(entrustVO.getTeu().add(teu));
                        }
                    }
                }
            }
            entrustVO.setContainerNoAndSealNoAndType(containerNoAndSealNoAndType.toString());
            entrustVO.setPackageQty(orderInfo4List.getNoOfPackage());
            entrustVO.setWeight(orderInfo4List.getGrossWeight());
            entrustVO.setTareWeight(tareWeight);
            entrustVO.setVolume(orderInfo4List.getVolume());
            //押金与海运费暂时不设
            entrustVO.setDepositPrice(new BigDecimal(0));
            entrustVO.setSellingPrice(new BigDecimal(0));
            //船名航次合计
            entrustVO.setVesselVoyage(orderInfo4List.getVessel() + " .V" + orderInfo4List.getVoyage());
            //时间Date类型转换为String，否则easyExcel会报错
            if (null != orderInfo4List.getEtd()) {
                entrustVO.setEtd(DateUtil.convertDateToString(orderInfo4List.getEtd()));
            }
            entrustVO.setDescription(orderInfo4List.getDescription());
            entrustVO.setHsCode(orderInfo4List.getHsCode());
            entrustVO.setShipper(orderInfo4List.getShipper());
            entrustVO.setConsignee(orderInfo4List.getConsignee());
            entrustVO.setNotify(orderInfo4List.getNotify());
            entrustVO.setPlaceOfReceiptName(orderInfo4List.getPlaceOfReceiptName());
            entrustVO.setPortOfLoadingName(orderInfo4List.getPortOfLoadingName());
            entrustVO.setPlaceOfDeliveryName(orderInfo4List.getPlaceOfDeliveryName());
            entrustVO.setPortOfDischargeName(orderInfo4List.getPortOfDischargeName());
            entrustVO.setDeliveryTermName(orderInfo4List.getDeliveryTermName());
            entrustVO.setPaymentMethodName(orderInfo4List.getPaymentMethodName());
            entrustVO.setPlaceOfPayName(orderInfo4List.getPlaceOfPayName());
            entrustVO.setDocumentTypeName(orderInfo4List.getDocumentTypeName());
            entrustVO.setBlIssuePlaceName(orderInfo4List.getBlIssuePlaceName());
            entrustVO.setBillRemark(orderInfo4List.getBillRemark());
            //一些标志位判断
            if (null != orderInfo4List.getIsSoc()) {
                entrustVO.setSoc(0 == orderInfo4List.getIsSoc() ? "未打标" : "已打标");
            }
            if (null != orderInfo4List.getIsAmendmentFeeCharging()) {
                entrustVO.setIsBlAmendment(orderInfo4List.getIsAmendmentFeeCharging() == 0 ? "未收费改单" : "已收费改单");
            }
            entrustVO.setIsRollOver(0 == orderInfo4List.getIsRollOver() ? "未甩货" : "已甩货");
            entrustVO.setIsBillSplit(0 == orderInfo4List.getIsBillSplit() ? "未拆票" : "拆票");
            entrustVO.setIsBillCombine(0 == orderInfo4List.getIsBillCombine() ? "未合票" : "合票");
            entrustVOList.add(entrustVO);
        });
        return entrustVOList;
    }
}
